package com.whut.cristiano.autographbook.Activity;


import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.whut.cristiano.autographbook.Fragment.DiscoverFragment;
import com.whut.cristiano.autographbook.Fragment.DrawFragment;
import com.whut.cristiano.autographbook.Fragment.MyInfoFragment;
import com.whut.cristiano.autographbook.R;

public class MainActivity extends BaseActivity implements BottomNavigationBar.OnTabSelectedListener,MyInfoFragment.OnFragmentInteractionListener,DrawFragment.OnFragmentInteractionListener {

    private static final String TAG = MainActivity.class.getSimpleName();
    private BottomNavigationBar bottomNavigationBar;
    private int lastSelectedPosition = 0;
    private DrawFragment drawFragment;
    private MyInfoFragment myInfoFragment;
    private DiscoverFragment discoverFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    @Override
    public void initView() {
        super.initView();
        bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.main_draw, "绘制").setActiveColorResource(R.color.orange))
                .addItem(new BottomNavigationItem(R.drawable.main_discover, "发现").setActiveColorResource(R.color.blue_color))
                .addItem(new BottomNavigationItem(R.drawable.main_me, "我").setActiveColorResource(R.color.green))
                .setFirstSelectedPosition(lastSelectedPosition)
                .initialise();
        bottomNavigationBar.setTabSelectedListener(this);
        setDefaultFragment();
    }

    @Override
    public void onTabSelected(int position) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        switch (position) {
            case 0:
                if (drawFragment == null) {
                    drawFragment = DrawFragment.newInstance("0","1");
                }
                transaction.replace(R.id.main_page_fragment, drawFragment);
                break;
            case 1:
                if (discoverFragment == null) {
                    discoverFragment = DiscoverFragment.newInstance("0","1");
                }
                transaction.replace(R.id.main_page_fragment, discoverFragment);
                break;
            case 2:
                if (myInfoFragment == null) {
                    myInfoFragment = new MyInfoFragment();
                }
                transaction.replace(R.id.main_page_fragment,myInfoFragment);
                break;
            default:
                break;
        }
        // 事务提交
        transaction.commit();
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }
    /**
     * 设置默认的
     */
    private void setDefaultFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        drawFragment = DrawFragment.newInstance("0","1");
        transaction.replace(R.id.main_page_fragment, drawFragment);
        transaction.commit();
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }
}
